www.gusucode.com > Java 编写画图工具源代码 > Java 编写画图工具源代码\www.gusucode.com\步骤1.txt
//实现了主要的框架和画图的功能 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MyPaint extends JFrame { class DrawingPanel extends JPanel{ //构造DrawingPanel实例 public DrawingPanel(){ setBackground(Color.white); addMouseListener(new MyMouseListener()); addMouseMotionListener(new MyMouseListener()); } public void paint(Graphics g){ g.setColor(color); g.drawLine(oldX, oldY, newX, newY); } class MyMouseListener extends MouseAdapter implements MouseMotionListener{ //单击响应记录座标 public void mousePressed(MouseEvent e){ oldX = newX = e.getX(); oldY = newY = e.getY(); } //拖动响应画出线条 public void mouseDragged(MouseEvent e){ oldX = newX; oldY = newY; newX = e.getX(); newY = e.getY(); repaint(); } } } public MyPaint(String title){ super(title); } //内容面板 Container container = getContentPane(); //座标的记录 int oldX, oldY, newX, newY; //画图面板 DrawingPanel panel = new DrawingPanel(); //画笔颜色 Color color = Color.blue; //生成JFrame public void go(){ setSize(500, 500); setLocation(200, 200); container.add(panel); setVisible(true); addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); } public static void main(String[] args) { //设定系统外观为默认外观 try{ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e){ System.out.print(e); } //生成主要的框架 MyPaint paint = new MyPaint("java实验 绘图工具 V2.0"); paint.go(); } }